package com.scm.biz.service._impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scm.biz.bean.Encasement;
import com.scm.biz.bean.EncasementItem;
import com.scm.biz.bean.Order;
import com.scm.biz.bean.OrderTaskPerformer;
import com.scm.biz.bean.ProductionOrder;
import com.scm.biz.bean.ProductionPattern;
import com.scm.biz.bean.SysUser;
import com.scm.biz.dao.CustomerDao;
import com.scm.biz.dao.EncasementDao;
import com.scm.biz.dao.EncasementItemDao;
import com.scm.biz.dao.OrderDao;
import com.scm.biz.dao.OrderTaskPerformerDao;
import com.scm.biz.dao.ProductionOrderDao;
import com.scm.biz.dao.ProductionPatternDao;
import com.scm.biz.dto.EncasementDto;
import com.scm.biz.dto.MsgNoticeDto;
import com.scm.biz.enums.OrderStatusEnum;
import com.scm.biz.enums.OrderTypeEnum;
import com.scm.biz.result.EncasementResult;
import com.scm.biz.service.EncasementService;
import com.scm.biz.service.MsgNoticeService;
import com.scm.biz.service.OrderTaskFlowService;
import com.scm.biz.service.OrderTaskPerformerService;
import com.scm.biz.service.ProductionPatternService;
import com.scm.utils.UfdmDateUtil;
@Service
public class EncasementServiceImpl implements EncasementService {
	@Autowired
	EncasementDao encasementDao;
	@Autowired
	EncasementItemDao encasementItemDao;
	@Autowired
	ProductionPatternService productionPatternService;
	@Autowired
	OrderTaskFlowService orderTaskFlowService;
	@Autowired
	ProductionOrderDao productionOrderDao;
	@Autowired
	OrderDao orderDao;
	@Autowired
	MsgNoticeService msgNoticeService;
	@Autowired
	CustomerDao customerDao;
	@Autowired
	OrderTaskPerformerService orderTaskPerformerService;
	@Override
	public void update(Encasement encasement) {
		encasementDao.updateByPrimaryKeySelective(encasement);
	}

	@Override
	public void insert(EncasementDto encasementDto,Integer opFlag) {
		Encasement encasement = new Encasement();
		encasement.setDeliveryDate(UfdmDateUtil.stringToDate(encasementDto.getDeliveryDate(), "yyyy-MM-dd HH:mm"));
		encasement.setProductionPatternId(encasementDto.getProductionPatternId());
		encasement.setBatchNo(encasementDto.getBatchNo());
		encasement.setDeliver(encasementDto.getDeliver());
		encasementDao.insert(encasement);
		EncasementItem encasementItem = null;
		for(Map<String,String> encasementItemMap:encasementDto.getEncasementItems()){
			encasementItem = new EncasementItem();
			encasementItem.setColorCode(encasementItemMap.get("colorCode"));
			encasementItem.setGrossWeight(encasementItemMap.get("grossWeight"));
			encasementItem.setNetWeight(encasementItemMap.get("netWeight"));
			encasementItem.setPackageOrderId(encasement.getId());
			encasementItem.setpNo(encasementItemMap.get("pNo"));
			encasementItem.setSize(encasementItemMap.get("size"));
			if(encasementItemMap.get("lAmount")!=null && !encasementItemMap.get("lAmount").isEmpty()){
				encasementItem.setlAmount(Integer.valueOf(encasementItemMap.get("lAmount")));
			}
			if(encasementItemMap.get("mAmount")!=null && !encasementItemMap.get("mAmount").isEmpty()){
				encasementItem.setmAmount(Integer.valueOf(encasementItemMap.get("mAmount")));
			}
			if(encasementItemMap.get("sAmount")!=null && !encasementItemMap.get("sAmount").isEmpty()){
				encasementItem.setsAmount(Integer.valueOf(encasementItemMap.get("sAmount")));
			}
			if(encasementItemMap.get("total")!=null && !encasementItemMap.get("total").isEmpty()){
				encasementItem.setTotal(Integer.valueOf(encasementItemMap.get("total")));
			}
			if(encasementItemMap.get("xlAmount")!=null && !encasementItemMap.get("xlAmount").isEmpty()){
				encasementItem.setXlAmount(Integer.valueOf(encasementItemMap.get("xlAmount")));
			}
			if(encasementItemMap.get("total")!=null && !encasementItemMap.get("total").isEmpty()){
				encasementItem.setTotal(Integer.valueOf(encasementItemMap.get("total")));
			}
			encasementItemDao.insert(encasementItem);
		}
		
			ProductionPattern productionPattern = productionPatternService.selectById(encasementDto.getProductionPatternId());
			if(productionPattern.getStatus()<=OrderStatusEnum.PRODUCTION_QS.value()){
				productionPattern.setStatus(OrderStatusEnum.PRODUCTION_QS.value());
				ProductionOrder productionOrder = productionOrderDao.selectByPrimaryKey(productionPattern.getProductionOrderId());
				Order order = orderDao.selectByPrimaryKey(productionOrder.getOrderId());
				
				productionPatternService.update(productionPattern);
				orderTaskFlowService.addPerformer(OrderTypeEnum.T3, "F304",order.getManager(), productionPattern.getId());
				SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");

				OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
				orderTaskPerformer.setAction("F303");
				orderTaskPerformer.setBusinessId(productionPattern.getId());
				orderTaskPerformer.setBusinessType(OrderTypeEnum.T3.toString());
				orderTaskPerformer.setCreatedAt(new Date());
				orderTaskPerformer.setPerformer(sysUser.getId());
				orderTaskPerformer.setUpdatedAt(new Date());
				orderTaskPerformer.setOrderId(order.getId());
				orderTaskPerformerService.addPerformer(orderTaskPerformer);
				/********************消息***************/
			
				
				MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
				msgNoticeDto.setTitle("工厂已发货");
				msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
										+ "客户的大货订单已发货，快递单号为"+encasement.getBatchNo());
				msgNoticeDto.setOrderType(OrderTypeEnum.T3.toString());
				msgNoticeDto.setTaskId(productionOrder.getId());
				msgNoticeDto.setUserType(0);
				msgNoticeDto.setOrderId(order.getId());
				msgNoticeService.insert(msgNoticeDto,productionOrder.getCreator());
			}
			
		
	}

	@Override
	public List<EncasementResult> selectListByProductionPatternId(
			Long productionPatternId) {
		// TODO Auto-generated method stub
		return encasementDao.selectListByProductionPatternId(productionPatternId);
	}

	@Override
	public Encasement selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return encasementDao.selectByPrimaryKey(id);
	}

	@Override
	public void saveArriveDate(Long productionPatternId,Long encasementId, String arriveDate) {
		ProductionOrder productionOrder = productionOrderDao.selectByPrimaryKey(productionPatternService.selectById(productionPatternId).getProductionOrderId());

		Order order = orderDao.selectByPrimaryKey(productionOrder.getOrderId());
		if(arriveDate !=null){
			Encasement encasement = encasementDao.selectByPrimaryKey(encasementId);
			encasement.setArriveDate(UfdmDateUtil.stringToDate(arriveDate, "yyyy-MM-dd"));
		
			encasementDao.updateByPrimaryKey(encasement);
			
			/********************消息***************/
			
			MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
			msgNoticeDto.setTitle("大货已送达");
			msgNoticeDto.setContent(customerDao.selectByPrimaryKey(order.getCustomerId().intValue()).getFullname()
									+ "客户的大货订单已送达，到货时间为"+encasement.getBatchNo()+"，请联系客户付款");
			msgNoticeDto.setOrderType(OrderTypeEnum.T3.toString());
			msgNoticeDto.setTaskId(productionOrder.getId());
			msgNoticeDto.setUserType(0);
			msgNoticeDto.setOrderId(order.getId());
			msgNoticeService.insert(msgNoticeDto,productionOrder.getCreator());
		}
		
			

	
	}
	@Override
	public void saveArriveDateFinish(Long productionPatternId) {
		ProductionOrder productionOrder = productionOrderDao.selectByPrimaryKey(productionPatternService.selectById(productionPatternId).getProductionOrderId());

		Order order = orderDao.selectByPrimaryKey(productionOrder.getOrderId());
		
		ProductionPattern productionPattern = productionPatternService.selectById(productionPatternId);
		if(productionPattern.getStatus() <=OrderStatusEnum.PRODUCTION_SF.value()){
			productionPattern.setStatus(OrderStatusEnum.PRODUCTION_SF.value());
			productionPatternService.update(productionPattern);
			orderTaskFlowService.addPerformer(OrderTypeEnum.T3, "F305",order.getManager(), productionPatternId);
			SysUser sysUser = (SysUser)SecurityUtils.getSubject().getSession().getAttribute("sysUser");

			OrderTaskPerformer orderTaskPerformer = new OrderTaskPerformer();
			orderTaskPerformer.setAction("F304");
			orderTaskPerformer.setBusinessId(productionPattern.getId());
			orderTaskPerformer.setBusinessType(OrderTypeEnum.T3.toString());
			orderTaskPerformer.setCreatedAt(new Date());
			orderTaskPerformer.setPerformer(sysUser.getId());
			orderTaskPerformer.setUpdatedAt(new Date());
			orderTaskPerformer.setOrderId(order.getId());
			orderTaskPerformerService.addPerformer(orderTaskPerformer);
			
		}
			

	
	}
	
	

}
